﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BTKERP.IBLL.Inventory;
using BTKERP.BLL.Inventory;
using BTKERP.Model;

namespace BTKERP.MVCWeb.Controllers
{
    public class WarehouseController : RestrictController
    {
        //
        // GET: /Warehouse/
        public ActionResult Index()
        {
            List<Warehouse> warehouses = InventoryInfoMgt.GetWarehouses();
            return View(warehouses);
        }

        //
        // GET: /Warehouse/Details/5

        public ActionResult Details(Guid id)
        {
            Warehouse warehouse = new Warehouse();
            warehouse = InventoryInfoMgt.GetWarehouseById(id);
            return View(warehouse);
        }

        //
        // GET: /Warehouse/Create

        public ActionResult Create()
        {
            Warehouse warehouse = new Warehouse();
            ViewData["CountryList"] = GetCoutrySelectList();
            ViewData["CitiyList"] = GetCitySelectList();
            return View(warehouse);
        } 

        //
        // POST: /Warehouse/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Warehouse warehouse = new Warehouse();
                UpdateModel(warehouse);
                InventoryInfoMgt.AddWarehouse(warehouse.Name, warehouse.Descriptoin, warehouse.IsCentral, warehouse.Address1, warehouse.Address2, warehouse.Address3,
                    warehouse.PostCode, warehouse.Tel, warehouse.Fax, warehouse.ContactPerson, warehouse.CoutryId, warehouse.CityId, SessionEntity.Staff.UserName);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Warehouse/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            Warehouse house = InventoryInfoMgt.GetWarehouseById(id);
            ViewData["CountryList"] = GetCoutrySelectList();
            ViewData["CitiyList"] = GetCitySelectList();
            return View(house);
        }

        //
        // POST: /Warehouse/Edit/5

        [HttpPost]
        public ActionResult Edit(FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                Warehouse house = new Warehouse();
                UpdateModel(house);
                InventoryInfoMgt.UpdateWarehouse(house.Id, house.Name, house.Descriptoin, house.IsCentral, house.Address1, house.Address2, house.Address3,
                    house.PostCode, house.Tel, house.Fax, house.ContactPerson, house.CoutryId, house.CityId, SessionEntity.Staff.UserName);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Warehouse/Delete/5
 
        public ActionResult Delete(Guid id)
        {
            Warehouse house = InventoryInfoMgt.GetWarehouseById(id);
            return View(house);
        }

        //
        // POST: /Warehouse/Delete/5

        [HttpPost]
        public ActionResult Delete(Warehouse house)
        {
            try
            {
                // TODO: Add delete logic here
                InventoryInfoMgt.DeleteWarehouse(house.Id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
